package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetMealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/setmeal")
@Api(tags = "套餐相关接口")
@Slf4j
public class SetMealControllor {
    @Autowired
    private SetMealService setMealService;

    @ApiOperation("新增套餐")
    @PostMapping
    @CacheEvict(value = "setmealCache",key = "#dto.categoryId")
    public Result add(@RequestBody SetmealDTO dto) {
        log.info("新增套餐:{}", dto);
        setMealService.insert(dto);
        return Result.success();
    }

    @ApiOperation("根据id查询套餐")
    @GetMapping("/{id}")
    public Result<SetmealVO> selectById(@PathVariable Long id) {
        log.info("id查询套餐:{}", id);
        SetmealVO vo = setMealService.selectWithDishesById(id);
        return Result.success(vo);
    }

    @ApiOperation("套餐分页查询")
    @GetMapping("/page")
    public Result<PageResult> pageQuery(SetmealPageQueryDTO dto) {
        log.info("套餐分页查询：{}", dto);
        PageResult pageResult = setMealService.pageQuery(dto);
        return Result.success(pageResult);

    }
    @ApiOperation("批量删除套餐")
    @DeleteMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public Result deleteBatchByIds(@RequestParam List<Long> ids){
        log.info("删除套餐:{}",ids);
        setMealService.deleteBatchByIds(ids);
        return Result.success();
    }

    @ApiOperation("套餐起售、停售")
    @PostMapping("/status/{status}")
    @CacheEvict(value = "setmealCache",allEntries = true)
    public Result stopOrStart(@PathVariable Integer status, Long id) {
        log.info("更改套餐{}状态为:{}", id,status);
        setMealService.stopOrStart(status, id);
        return Result.success();
    }
    @ApiOperation("修改套餐")
    @PutMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public Result update(@RequestBody SetmealDTO dto){
        log.info("修改套餐为:{}",dto);
        setMealService.update(dto);
        return Result.success();
    }
}
